/***************************************************************************
 *   Copyleft 2006-2007 by												   *
 *	 Iwanicki Krzysztof													   *
 *   <gamma.mini.pw.edu.pl/~iwanickik>									   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 ***************************************************************************/
#ifndef __BSPLINECURVE_H__
#define __BSPLINECURVE_H__

#include "./BezierCurve.h"
#include "../../../Utils/DynamicArray.h"

#define OBJECT_BSPLINE 4
#define BSPLINE_M_PER_POINT 0.07f	// number of partitions per pixel for the longest edge in De Boor's polygon
#define BSPLINE_PREC_DIFF 5			// difference between number of partitions needed to recalculate the mBasis


class BSplineCurve: public BezierCurve {
private:
	struct BSplineBasisW3 {
		cFloat n0;
		cFloat n1;
		cFloat n2;
		cFloat n3;
	};
	DynamicArray<BSplineBasisW3> mBasis;
	cInt mBasisPrecision;

	cVoid AddContPoints();									// creates 3 cont. points in bezier mode
	BSplineBasisW3 CalculateN(cFloat *T, cFloat t, cInt i);	// calculate b-spline mBasis
	cVoid AdaptBasis(Point4D *tab, cInt tabSize);				// research and change b-spline mBasis precision

protected:
	DynamicArray<PointPair> mBSplineNodes;
	DynamicArray<PointPair> mBNodes;
	cBool mBSplineBasis;
	cBool mBSplineMode;

	cVoid ConvertFromBSpline();
	cVoid ConvertToBSpline();
	cVoid CalculateBasis();
	cVoid RenderSpline(Renderer& renderer, Matrix4D& matrix, ProjectionPlane& plane, cWinColor color,
		cWinColor polygonColor, Point4D *tab, cInt tabSize);
public:

    /**************************************************************
    * Constructors                                                *
    ***************************************************************/
    BSplineCurve();
	BSplineCurve(cBool arePointsVisible, cBool isPolygonVisible, cWinColor color);
        
    /**************************************************************
    * Operators                                                   *
    ***************************************************************/


    /**************************************************************
    * Other helpful functions                                     *
    ***************************************************************/
	virtual Point4D GetCurvePoint(cInt iIndex);
	virtual cInt GetCurveNrOfPoints();

	cVoid SelectPoint(cInt iIndex, SELECTION_TYPE eSelected);
	SELECTION_TYPE SelectPoint(cInt iIndex, cBool bSelect);
	cVoid DeselectPoints();

	virtual cChar* GetName();
	virtual cVoid DisplayInfo(Renderer& renderer);
	cVoid DisplayInfo(Renderer& renderer, Matrix4D& matrix, ProjectionPlane& plane);
	cVoid RenderAbstract(Renderer& renderer, Matrix4D& matrix, ProjectionPlane& plane);
	cVoid RenderAbstract(Renderer& renderer, Matrix4D& matrix, ProjectionPlane& plane, cWinColor color);
	virtual cVoid Render(Renderer& renderer, Matrix4D& matrix, ProjectionPlane& plane);
	virtual cVoid Render(Renderer& renderer, Matrix4D& matrix, ProjectionPlane& plane, cWinColor color);
	
	virtual cVoid AddPoint(Point4D point);
	virtual cVoid AddPoint(Point4D point, cUShort selected);
	virtual cVoid ReplacePoint(const Point4D& point, cInt iIndex);
	virtual cVoid RemovePoint(cInt i);
	Point4D GetPoint(cInt i);
	cInt GetNrOfPoints();

	cBool IfBSplineBasis();
	cVoid SetIfBSplineBasis(cBool v);
	cBool IfBSplineMode();
	cVoid SetIfBSplineMode(cBool v);
	cInt GetPrecision();
	cVoid SetPrecision(cInt prec);
	cVoid SwitchMode();
};                                                                 

#endif // __BSPLINECURVE_H__